// pages/persionalPage/persionalPage.js
//获取应用实例
const app = getApp()
var result = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isMan: true,
    isWoman: false,
    anymore: true,
    gender: 0,
    age: "请输入年龄",
    email: "输入邮箱",
    qq: "输入qq账号",
    wechat: "输入微信账号",
    phone:"输入手机号码",
    history: "暂无反馈记录",
    message: ""
  },

  loadHistory: function(){
    var that = this;
    //加载反馈记录
    wx.request({
      url: 'https://api.jiemengshi.com/wechat/feedback/list',
      header: {
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
      },
      success: function (res) {
        result = res.data.data
        console.log(result)

        var historyStr = "";
        //拼装反馈记录
        if (result.length > 0) {
          historyStr = "";
          for (var i = 0; i < result.length; i++) {
            historyStr += "我：";
            historyStr += result[i].content + "\n";
            var replies = result[i].replies;
            for (var j = 0; j < replies.length; j++) {
              var reply = replies[j];
              historyStr += "后台回复：";
              historyStr += reply.content + "\n";
            }
          }
        }

        if (result.length >= 2) {
          that.setData({
            anymore: false,
            message: "已达反馈次数上限"
          })
        }


        console.log(that.data.anymore)

        that.setData({
          history: historyStr
        })
      },
      fail: function () {
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },

  //联系我们事件
  contact : function(){
    wx.navigateTo({
      url: '../information/information'
    })
  },

  //绑定反馈输入事件
  listenerMessageInput: function (e) {
    this.setData({
      message: e.detail.value
    })
  },

  //留言
  leaveMessage: function () {
    if (this.data.message == "") {
      wx.showModal({
        title: '错误说明',
        content: "请输入反馈内容",
        showCancel: false
      })
    }
    else {
      var that = this;
      //发起网络请求
      wx.request({
        url: 'https://api.jiemengshi.com/wechat/feedback/add',
        data: {
          content: that.data.message
        },
        header: {
          'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '谢谢，梦友',
          })
          that.setData({
            message: ""
          })
          that.loadHistory()
        },
        fail: function () {
          wx.showToast({
            title: '反馈失败',
          })
        }
      })
    }
  },

  onShareAppMessage: function (res) {
    return {
      title: '解梦师',
      path: '/pages/mengjingku/mengjingku',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1200
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          icon: 'loading',
          duration: 1200
        })
      }
    }
  },

  onLoad: function (options) {
    //显示转发
    wx.showShareMenu({
      withShareTicket: true
    })
    this.setData({
      code: app.globalData.code
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    //请求获取用户之前的信息
    var that = this;
    //网络请求解析
    wx.request({
      url: 'https://api.jiemengshi.com/jms/wechat/user/get',
      header: {
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
      },
      success: function (res) {
        var v0 = "请输入年龄"
        var v1 = "输入邮箱"
        var v2 = "输入qq账号" 
        var v3 = "输入微信账号"
        var v4 = "输入手机号码"
        result = res.data.data
        console.log(result)
        if(result.age != 0){
          v0 = result.age
        }
        if(result.email != ""){
          v1 = result.email
        }
        if (result.qqNumber != "") {
          v2 = result.qqNumber
        }
        if (result.wechatId != "") {
          v3 = result.wechatId
        }
        if (result.qqNumber != "") {
          v4 = result.telephone
        }
        //设置数据显示
        if(result.gender == 2){
          that.setData({
            isWoman: true,
            isMan: false
          })
        }
        else{
          that.setData({
            isWoman: false,
            isMan: true
          })
        }
        that.setData({
          age: v0,
          email: v1,
          qq: v2,
          wechat: v3,
          phone: v4
        })
      },
      fail: function () {
        wx.showToast({
          title: '请求失败',
        })
      }
    })

    this.loadHistory()
  },

  //年龄输入事件
  listenerAgeInput: function(e){
    this.setData({
      age: e.detail.value
    })
  },

  //邮箱输入事件
  listenerEmailInput: function (e) {
    this.setData({
      email: e.detail.value
    })
  },

  //性别选择事件
  radioChange : function(e){
    if(e.detail.value == "男"){
      this.setData({
        gender: 1
      })
    }
    else{
      this.setData({
        gender: 2
      })
    }
  },

  //qq输入事件
  listenerQQInput: function (e) {
    this.setData({
      qq: e.detail.value
    })
  },

  //微信输入事件
  listenerWechatInput: function (e) {
    this.setData({
      wechat: e.detail.value
    })
  },

  //手机输入事件
  listenerPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  modifyInfo: function(){
    var that = this;
    //去除默认值的传递
    if(this.data.age == "请输入年龄"){
      this.setData({
        age : 0
      })
    }
    if (this.data.email == "输入邮箱") {
      this.setData({
        email: ""
      })
    }
    if (this.data.qq == "输入qq账号") {
      this.setData({
        qq: ""
      })
    }
    if (this.data.wechat == "输入微信账号") {
      this.setData({
        wechat: ""
      })
    }
    if (this.data.phone == "输入手机号码") {
      this.setData({
        phone: ""
      })
    }

    //网络请求解析
    wx.request({
      url: 'https://api.jiemengshi.com/jms/wechat/user/update',
      header: {
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
      },
      data:{
        age : that.data.age,
        avatarUrl: result.avatarUrl,
        city: result.city,
        country: result.country,
        email: that.data.email,
        gender: that.data.gender,
        level: result.level,
        nickName: result.nickName,
        province: result.province,
        qqNumber: that.data.qq,
        telephone: that.data.phone,
        wechatId: that.data.wechat,
      },
      success: function (res) {
        wx.showToast({
          title: '更新成功！',
          duration: 500
        })
        wx.navigateBack({
          delta : 1
        })
      },
      fail: function () {
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})